#!/usr/bin/python
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# The script takes as an input a text file that contains one single ad unit ID per line
"""This code changes the target platform of the ad units from a file from WEB to MOBILE"""

__authors__ = ['felipel+bulk@google.com (Felipe Lora)']

import os
import sys
import time
from copy import deepcopy

sys.path.insert(0, os.path.join('..', '..', '..', '..'))

# Import appropriate classes from the client library.
from adspygoogle.common import Utils
from adspygoogle.dfp.DfpClient import DfpClient
from adspygoogle.dfp import DfpUtils
from adspygoogle.dfp.DfpErrors import *

# Initialize client object.
client = DfpClient(path=os.path.join('..', '..', '..', '..'))


# Initialize appropriate service. By default, the request is always made against
# the production environment.
inventory_service = client.GetInventoryService('https://www.google.com', 'v201111')
network_service = client.GetNetworkService('https://www.google.com', 'v201111')


#############################################################


# Read all the ad units to be created from the DoubleClick standard text file
def modifyAdUnits(auFile):

  fauFile = open(auFile,'rU')
  q = 0
  for line in fauFile.readlines():
    adUnitId = line.rsplit("\n")[0]
    adUnit = inventory_service.GetAdUnit(adUnitId)[0]
    adUnit['targetPlatform'] = 'MOBILE'
    result = inventory_service.updateAdUnit(adUnit)
    q = q + 1
  print "Finished updating %s ad units" % q
  return 0

def Main():
  # Read the ad units from the text file
  args = sys.argv[1:]
  auFile = args[0]
  adUnits = modifyAdUnits(auFile)

if __name__ == '__main__':
  Main()

